package com.gome.todo;

import com.gome.vue4j.core.AbstractVue4jComponent;
import com.gome.vue4j.core.FXMLPath;
import com.gome.vue4j.core.InvokeAbleMethod;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.fxml.FXML;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@FXMLPath("/todo/todo-list-view.fxml")
public class TodoList extends AbstractVue4jComponent {

    @FXML
    public List<StringProperty> todoList;

    private InvokeAbleMethod parentRemoveTodo;

    @FXML
    public void removeTodo(int index) throws InvocationTargetException, IllegalAccessException {
        parentRemoveTodo.invoke(index);
    }

    @Override
    public void setProps(Map<String, Object> props) {
        this.todoList = ((List<Todo>) props.get("list")).stream().map(e -> new SimpleStringProperty(e.getText())).collect(Collectors.toList());
        this.parentRemoveTodo = (InvokeAbleMethod) props.get("#removeTodo");
    }
}
